package com.ruding.disruptor.demo;

import java.nio.ByteBuffer;

import com.lmax.disruptor.RingBuffer;

public class LongEventProducer{
	//生产者持有RingBuffer实例，可以直接向RingBuffer实例中的entry写入数据
	private final RingBuffer<LongEvent> ringBuffer;

	public LongEventProducer(RingBuffer<LongEvent> ringBuffer)
	{
		this.ringBuffer = ringBuffer;
	}

	public void onData(long bb)
	{
		long sequence = ringBuffer.next();  // Grab the next sequence
		LongEvent event = null;
		try
		{
			//从ringBuffer实例中获取entry
			event = ringBuffer.get(sequence); // Get the entry in the Disruptor
			// for the sequence
			//生产者将数据写入entry
			event.set(bb);  // Fill with data
		}
		finally
		{
			//生产者向ringBuffer提交数据变更
			ringBuffer.publish(sequence);
		}
	}
}
